package topic.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;

import common.Constant;
import https.UpLoadFileController;
import topic.bean.DeliverCommentPictureBean;
import utils.BitmapUtils;
import utils.MyLogUtils;
import view.MyGridView;

/**
 * Created by Administrator on 2017/3/13.
 */

public class DeliverCommentGridViewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<DeliverCommentPictureBean> arrayList;
    private DeleteListener deleteListener;
    private static final String TAG = "DeliverCommentGridViewA";
    private HasUpLoad hasUpLoad;
    private LoadImaFail loadImaFail;


    public DeliverCommentGridViewAdapter(Context context, ArrayList<DeliverCommentPictureBean> paths, DeleteListener deleteListener, HasUpLoad hasUpLoad,LoadImaFail loadImaFail) {
        this.context = context;
        this.arrayList = paths;
        this.deleteListener = deleteListener;
        this.hasUpLoad = hasUpLoad;
        this.loadImaFail = loadImaFail;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(final int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyHolder myHolder;
        final BitmapUtils bitmapUtils = new BitmapUtils();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.deliver_blog_grid_item, null);
            myHolder = new MyHolder();
            myHolder.content = (ImageView) convertView.findViewById(R.id.deliver_comment_grid_item_content);
            myHolder.cancel = (ImageView) convertView.findViewById(R.id.deliver_comment_grid_item_cancel);
            myHolder.deliver_comment_grid_item_cover = convertView.findViewById(R.id.deliver_comment_grid_item_cover);
            convertView.setTag(myHolder);
        } else {
            myHolder = (MyHolder) convertView.getTag();
        }
        if (((MyGridView) parent).isOnMeasure) {
            //如果是onMeasure调用的就立即返回
            return convertView;
        } else {
            myHolder.cancel.setVisibility(View.GONE);//图片未上传完成前不能取消
            myHolder.cancel.setOnClickListener(v -> deleteListener.deleteListener(position));
            final ViewGroup.LayoutParams layoutParams = myHolder.deliver_comment_grid_item_cover.getLayoutParams();
            layoutParams.height = 180;
            myHolder.deliver_comment_grid_item_cover.setLayoutParams(layoutParams);
            final Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    switch (msg.what) {
                        case Constant.ERROR_CODE_SUCCESS:
                            try {
                                JSONObject jsonObject = new JSONObject((String) msg.obj);
                                String url = jsonObject.getString("url");
                                arrayList.get(position).setRemoteUrl(url);
                                MyLogUtils.e(TAG, "position" + position + "::::::" + arrayList.get(position).getRemoteUrl());
                                BitmapUtils.deleteBitmapFile(arrayList.get(position).getName());
                                hasUpLoad.hasUpLoad(position, true, url);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            myHolder.cancel.setVisibility(View.VISIBLE);
                            break;
                        case Constant.ERROR_CODE_UPLOADING:
                            double percent = (double) msg.obj;
                            int newHeight = (int) (180 * percent);
                            layoutParams.height = (180 - newHeight);
                            myHolder.deliver_comment_grid_item_cover.setLayoutParams(layoutParams);
                            break;
                        case 3://缓存图片搞定
                            Bitmap bitmap = (Bitmap) msg.obj;
                            if (bitmap != null) {
                                myHolder.content.setImageBitmap(bitmap);
                            } else {
                                MyLogUtils.e(TAG, "noPicture");
                                myHolder.content.setImageBitmap(null);
                                loadImaFail.loadImaFail();
                            }
                            if (arrayList.get(position).isHasUpLoad()) {
                                myHolder.cancel.setVisibility(View.VISIBLE);
                                layoutParams.height = 0;
                                myHolder.deliver_comment_grid_item_cover.setLayoutParams(layoutParams);
                            }
                            break;
                        default:
                            break;
                    }
                }
            };
            MyLogUtils.e(TAG, arrayList.get(position).getLocalUrl());
            BitmapUtils.decodeFile(arrayList.get(position).getLocalUrl(), bitmap -> {
                final Message message = new Message();
                message.what = 3;
                message.obj = bitmap;
                handler.sendMessage(message);
                if (!arrayList.get(position).isHasUpLoad()) {
                    //如果图片未被上传过
                            DeliverCommentPictureBean deliverCommentPictureBean = arrayList.get(position);
                            deliverCommentPictureBean.setName(UpLoadFileController.getImageRemoteUrl());//设置远程图片保存的url
                            bitmapUtils.saveBitmapFile(deliverCommentPictureBean.getLocalUrl(), file -> {
                                //上传图片到后台
                                bitmapUtils.upLoadIma(context, handler, file);
                            }, deliverCommentPictureBean.getName());
                        }
            }, 180, 180);
            return convertView;
        }
    }

    class MyHolder {
        ImageView content;
        ImageView cancel;
        View deliver_comment_grid_item_cover;
    }

    public interface LoadLocalImg {
        //缓存图片的接口
        void loadLocalImg(Bitmap bitmap);
    }

    public interface DeleteListener {
        //删除图片的接口
        void deleteListener(int position);
    }

    public interface LocalImgComplete {
        //本地图片保存成功接口
        void localImgComplete(File file);
    }

    public interface HasUpLoad {
        //是否已经上传图片了
        void hasUpLoad(int position, boolean hasUpload, String url);
    }

    public interface LoadImaFail{
        //读取本地图片失败
        void loadImaFail();
    }
}
